import datetime
from xtquant import xtconstant
from jili.xtlib import xtdict
def xttime2datetime(i):
    return datetime.datetime.fromtimestamp(i/1000)

def xtcode2obj(code):
    return code.split(".")
def obj2xtcode(obj,mkcode=None):
    #支持港股通,上海,深圳,北京
    if mkcode is None:
        if len(obj)==6:
            if obj.startswith("6") or obj.startswith("5"):
                mkcode="SH"
            elif obj.startswith("0") or obj.startswith("3") or obj.startswith("1"):
                mkcode="SZ"
            else:
                mkcode = "BJ"
        elif len(obj)==5:
            mkcode="HK"
    if mkcode is None:
        code=obj
    else:
        code=obj+"."+mkcode
    return code
def ats2xt_price_type(price_type):
    if price_type=="market":
        return xtconstant.LATEST_PRICE
    else:
        return xtconstant.FIX_PRICE
def ats2xt_order_type(openclose,bs):
    """
    融资买入 - xtconstant.CREDIT_FIN_BUY
    融券卖出 - xtconstant.CREDIT_SLO_SELL
    买券还券 - xtconstant.CREDIT_BUY_SECU_REPAY
    直接还券 - xtconstant.CREDIT_DIRECT_SECU_REPAY
    卖券还款 - xtconstant.CREDIT_SELL_SECU_REPAY
    直接还款 - xtconstant.CREDIT_DIRECT_CASH_REPAY
    """
    order_type=None
    if openclose=="open":
        if bs=="buy":
            order_type=xtconstant.STOCK_BUY
        elif bs=="sell":
            order_type=xtconstant.CREDIT_SLO_SELL
    elif openclose=="close":
        if bs=="buy":
            order_type=xtconstant.CREDIT_BUY_SECU_REPAY
        elif bs=="sell":
            order_type=xtconstant.STOCK_SELL
    elif openclose=="debit":
        if bs=="buy":
            order_type=xtconstant.CREDIT_FIN_BUY
        elif bs=="sell":
            order_type=xtconstant.CREDIT_SELL_SECU_REPAY
    return order_type
def xt2openclose(order_type,price_type):
    info=""
    if order_type in [xtconstant.STOCK_BUY,xtconstant.CREDIT_BUY]:
        openclose, bs="open", "buy"
    elif order_type in [xtconstant.STOCK_SELL,xtconstant.CREDIT_SELL]:
        openclose, bs="close", "sell"
    elif order_type ==xtconstant.CREDIT_FIN_BUY:
        openclose, bs="debit", "buy"
    elif order_type ==xtconstant.CREDIT_SLO_SELL:
        openclose, bs="open", "sell"
    elif order_type ==xtconstant.CREDIT_BUY_SECU_REPAY:
        openclose, bs = "close", "buy"
    elif order_type ==xtconstant.CREDIT_SELL_SECU_REPAY:
        openclose, bs = "debit", "sell"
    else:
        openclose, bs = None, None
        print("sys","不认识的指令",order_type,price_type)
        info="不认识的指令"+order_type,price_type
    info=info+xtdict.code2name_order_type[order_type]+","+str(price_type)#xtdict.code2name_oprice_type[price_type]
    """
    股票
    
    买入 - xtconstant.STOCK_BUY
    卖出 - xtconstant.STOCK_SELL
    信用
    
    担保品买入 - xtconstant.CREDIT_BUY
    担保品卖出 - xtconstant.CREDIT_SELL
    融资买入 - xtconstant.CREDIT_FIN_BUY
    融券卖出 - xtconstant.CREDIT_SLO_SELL
    买券还券 - xtconstant.CREDIT_BUY_SECU_REPAY
    直接还券 - xtconstant.CREDIT_DIRECT_SECU_REPAY
    卖券还款 - xtconstant.CREDIT_SELL_SECU_REPAY
    直接还款 - xtconstant.CREDIT_DIRECT_CASH_REPAY
    专项融资买入 - xtconstant.CREDIT_FIN_BUY_SPECIAL
    专项融券卖出 - xtconstant.CREDIT_SLO_SELL_SPECIAL
    专项买券还券 - xtconstant.CREDIT_BUY_SECU_REPAY_SPECIAL
    专项直接还券 - xtconstant.CREDIT_DIRECT_SECU_REPAY_SPECIAL
    专项卖券还款 - xtconstant.CREDIT_SELL_SECU_REPAY_SPECIAL
    专项直接还款 - xtconstant.CREDIT_DIRECT_CASH_REPAY_SPECIAL
    期货六键风格
    
    开多 - xtconstant.FUTURE_OPEN_LONG
    平昨多 - xtconstant.FUTURE_CLOSE_LONG_HISTORY
    平今多 - xtconstant.FUTURE_CLOSE_LONG_TODAY
    开空 - xtconstant.FUTURE_OPEN_SHORT
    平昨空 - xtconstant.FUTURE_CLOSE_SHORT_HISTORY
    平今空 - xtconstant.FUTURE_CLOSE_SHORT_TODAY
    期货四键风格
    
    平多，优先平今 - xtconstant.FUTURE_CLOSE_LONG_TODAY_FIRST
    平多，优先平昨 - xtconstant.FUTURE_CLOSE_LONG_HISTORY_FIRST
    平空，优先平今 - xtconstant.FUTURE_CLOSE_SHORT_TODAY_FIRST
    平空，优先平昨 - xtconstant.FUTURE_CLOSE_SHORT_HISTORY_FIRST
    期货两键风格
    
    卖出，如有多仓，优先平仓，优先平今，如有余量，再开空 - xtconstant.FUTURE_CLOSE_LONG_TODAY_HISTORY_THEN_OPEN_SHORT
    卖出，如有多仓，优先平仓，优先平昨，如有余量，再开空 - xtconstant.FUTURE_CLOSE_LONG_HISTORY_TODAY_THEN_OPEN_SHORT
    买入，如有空仓，优先平仓，优先平今，如有余量，再开多 - xtconstant.FUTURE_CLOSE_SHORT_TODAY_HISTORY_THEN_OPEN_LONG
    买入，如有空仓，优先平仓，优先平昨，如有余量，再开多 - xtconstant.FUTURE_CLOSE_SHORT_HISTORY_TODAY_THEN_OPEN_LONG
    买入，不优先平仓 - xtconstant.FUTURE_OPEN
    卖出，不优先平仓 - xtconstant.FUTURE_CLOSE
    期货 - 跨商品套利
    
    开仓 - xtconstant.FUTURE_ARBITRAGE_OPEN
    平, 优先平昨 - xtconstant.FUTURE_ARBITRAGE_CLOSE_HISTORY_FIRST
    平, 优先平今 - xtconstant.FUTURE_ARBITRAGE_CLOSE_TODAY_FIRST
    期货展期
    
    看多, 优先平昨 - xtconstant.FUTURE_RENEW_LONG_CLOSE_HISTORY_FIRST
    看多，优先平今 - xtconstant.FUTURE_RENEW_LONG_CLOSE_TODAY_FIRST
    看空，优先平昨 - xtconstant.FUTURE_RENEW_SHORT_CLOSE_HISTORY_FIRST
    看空，优先平今 - xtconstant.FUTURE_RENEW_SHORT_CLOSE_TODAY_FIRST
    股票期权
    
    买入开仓，以下用于个股期权交易业务 - xtconstant.STOCK_OPTION_BUY_OPEN
    卖出平仓 - xtconstant.STOCK_OPTION_SELL_CLOSE
    卖出开仓 - xtconstant.STOCK_OPTION_SELL_OPEN
    买入平仓 - xtconstant.STOCK_OPTION_BUY_CLOSE
    备兑开仓 - xtconstant.STOCK_OPTION_COVERED_OPEN
    备兑平仓 - xtconstant.STOCK_OPTION_COVERED_CLOSE
    认购行权 - xtconstant.STOCK_OPTION_CALL_EXERCISE
    认沽行权 - xtconstant.STOCK_OPTION_PUT_EXERCISE
    证券锁定 - xtconstant.STOCK_OPTION_SECU_LOCK
    证券解锁 - xtconstant.STOCK_OPTION_SECU_UNLOCK
    期货期权
    
    期货期权行权 - xtconstant.OPTION_FUTURE_OPTION_EXERCISE
    ETF申赎
    
    申购 - xtconstant.ETF_PURCHASE
    赎回 - xtconstant.ETF_REDEMPTION
    """
    return openclose,bs,info

def xtorder_status2order_status(order_status):
    """
    xtconstant.ORDER_UNREPORTED	48	未报
    xtconstant.ORDER_WAIT_REPORTING	49	待报
    xtconstant.ORDER_REPORTED	50	已报
    xtconstant.ORDER_REPORTED_CANCEL	51	已报待撤
    xtconstant.ORDER_PARTSUCC_CANCEL	52	部成待撤
    xtconstant.ORDER_PART_CANCEL	53	部撤（已经有一部分成交，剩下的已经撤单）
    xtconstant.ORDER_CANCELED	54	已撤
    xtconstant.ORDER_PART_SUCC	55	部成（已经有一部分成交，剩下的待成交）
    xtconstant.ORDER_SUCCEEDED	56	已成
    xtconstant.ORDER_JUNK	57	废单
    xtconstant.ORDER_UNKNOWN	255	未知
    submitting,working,canceled,parttraded,alltraded,rejected
    """
    return xtdict.code2order_status[order_status]
def xt2dict(c):
    cc={}
    for i in dir(c):
        if i.startswith("_"):
            continue
        cc[i]=getattr(c,i)
    return cc